//
//  YSCustomerServiceCtrl.m
//  Yueshi
//
//  Created by yueshi－mac1 on 16/1/22.
//  Copyright © 2016年 悦食中国. All rights reserved.
//

#import "YSCustomerServiceCtrl.h"
#import "YSCustomerServicePhoneView.h"
#import "YSCustomerServiceCell.h"
#import "YSMineDataTool.h"
#import "YSCommonProblem.h"
#import "YSProblemDetailsCtrl.h"
#import "YSServiceChatCtrl.h"

@interface YSCustomerServiceCtrl ()<UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *ShoppingProblemBtn;
@property (weak, nonatomic) IBOutlet UIButton *LogisticsProblemBtn;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *shoppingProblems;
@property (strong, nonatomic) NSMutableArray *logisticsProblems;

@property (copy, nonatomic) NSString *phone;
@property (copy, nonatomic) NSString *workingTime;
@property (weak, nonatomic) IBOutlet UIImageView *badgeView;

@end

@implementation YSCustomerServiceCtrl

- (NSMutableArray *)shoppingProblems{
    if (!_shoppingProblems) {
        _shoppingProblems = [NSMutableArray array];
    }
    return _shoppingProblems;
}

- (NSMutableArray *)logisticsProblems{
    if (!_logisticsProblems) {
        _logisticsProblems = [NSMutableArray array];
    }
    return _logisticsProblems;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"联系客服";
    [self initView];
    [self requestData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealNewMessageBadge) name:kDealNewMessageBadgeNortif object:nil];
}

-(void)dealNewMessageBadge{
    if (ISLOGIN){
        self.badgeView.hidden = ![DefaultsHelper boolForKey:kHasNewChatMessage];
    }
}

- (void)requestData{
    [YSMineDataTool getCustomerService:^(YSCustomerServiceRes *result) {
        if (result.isSucc) {
            [self.shoppingProblems addObjectsFromArray:result.shopList];
            [self.logisticsProblems addObjectsFromArray:result.logisticsList];
            
            self.phone = result.consumerHotline;
            self.workingTime = result.operatingHours;
            
            [self initData];
        }else{
            ShowTips(result.data);
        }
    } failure:^(NSError *error) {
        ShowTips(kRequestFailure);
    }];
}

- (void) initView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YSCustomerServiceCell" bundle:nil]
         forCellReuseIdentifier:@"YSCustomerServiceCell"];
    
    self.ShoppingProblemBtn.selected = YES;
}

- (void) initData{
    YSCustomerServicePhoneView *footerView = [YSCustomerServicePhoneView instance];
    
    __weak typeof (self) weakSelf = self;
    footerView.clickAction = ^(){
        //打电话
//        NSString *phoneCode=[NSString stringWithFormat:@"tel:%@",self.phone];
//        UIWebView *callWebview = [[UIWebView alloc] init];
//        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:phoneCode]]];
//        [self.view addSubview:callWebview];
        
        UIAlertView *alert =  [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"确定拨打电话%@",weakSelf.phone] message:@"" delegate:weakSelf cancelButtonTitle:@"取消" otherButtonTitles:@"呼叫", nil];
        [alert show];
        
    };
    footerView.phone = self.phone;
    footerView.workingTime = self.workingTime;
    [self.tableView setTableFooterView:footerView];
    
    [self.tableView reloadData];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self dealNewMessageBadge];
}

- (IBAction)feedback:(id)sender {
    YSServiceChatCtrl *ctrl = [YSServiceChatCtrl new];
    [self.navigationController pushViewController:ctrl animated:YES];
}

- (IBAction)onClick:(UIButton *)sender {
    if (sender.selected) return;
    
    if (sender.tag) {
        sender.selected = YES;
        self.ShoppingProblemBtn.selected = NO;
    }else{
        sender.selected = YES;
        self.LogisticsProblemBtn.selected = NO;
    }
    
    [self.tableView reloadData];
}

#pragma mark tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.ShoppingProblemBtn.selected) {
        return self.shoppingProblems.count;
    }
    return self.logisticsProblems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSCustomerServiceCell *cell = [YSCustomerServiceCell cellWithTableView:tableView];
    if (self.ShoppingProblemBtn.selected) {
        YSCommonProblem *problem = self.shoppingProblems[indexPath.row];
        cell.text = problem.val;
    }else{
         cell.text = [self.logisticsProblems[indexPath.row] val];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YSCommonProblem *problem = nil;
    if (self.ShoppingProblemBtn.selected) {
        problem = self.shoppingProblems[indexPath.row];
    }else{
        problem = self.logisticsProblems[indexPath.row];
    }
    
    YSProblemDetailsCtrl *ctrl = [[YSProblemDetailsCtrl alloc] initWithProblem:problem];
    [self.navigationController pushViewController:ctrl animated:YES];
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        NSString *phoneCode=[NSString stringWithFormat:@"tel:%@",self.phone];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneCode]];
    }
}


@end
